package com.thread;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.LockSupport;

public class JUC_taotao_interview_LockSupport_22 {
    volatile List<Object> list = new ArrayList<>();


    void add(Object o){
        list.add(o);
    }
    int size(){
        return list.size();
    }
    static Thread t2 = null;
    public static void main(String[] args) {
        final Object lock = new Object();

        JUC_taotao_interview_LockSupport_22 t = new JUC_taotao_interview_LockSupport_22();
        CountDownLatch c = new CountDownLatch(1);


                Thread t1 = new Thread(() -> {
            System.out.println("t1 启动" );

            if(t.size() != 5){
                try {
                    LockSupport.park();

                    LockSupport.unpark(t2);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

            System.out.println("t1 end");
        }, "t1");

        try {
            TimeUnit.SECONDS.sleep(1);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        t2 = new Thread(() -> {
            for(int i=0;i<10;i++){

                t.add(new Object());
                System.out.println("add "+i);

                if(t.size() == 5){
                    LockSupport.unpark(t1);
                    LockSupport.park();
                }
                    /*
                    try {
                        TimeUnit.SECONDS.sleep(1);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }*/
            }
        }, "t2");

        t1.start();
        t2.start();
    }
}
